package com.javaxiaobear.module.monitor.mapper;

import cn.hutool.core.collection.ListUtil;
import com.javaxiaobear.base.common.utils.PageUtils;
import com.javaxiaobear.module.monitor.domain.SysLogininfor;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.SqlOperators;
import java.util.List;
import org.apache.ibatis.annotations.Delete;

/**
 * 系统访问日志情况信息 数据层
 *
 * @author javaxiaobear
 */
public interface SysLogininforMapper extends BaseMapper<SysLogininfor> {

  /**
   * 查询系统登录日志集合
   *
   * @param logininfor 访问日志对象
   * @return 登录记录集合
   */
  default List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
    return selectListByQuery(
        QueryWrapper.create(
                logininfor,
                SqlOperators.of(SysLogininfor::getUserName, SqlOperator.LIKE)
                    .of(SysLogininfor::getIpaddr, SqlOperator.LIKE))
            .ge(
                SysLogininfor::getLoginTime,
                logininfor.getParams().get("beginTime"),
                logininfor.getParams().get("beginTime") != null)
            .le(
                SysLogininfor::getLoginTime,
                logininfor.getParams().get("endTime"),
                logininfor.getParams().get("endTime") != null)
            .orderBy(SysLogininfor::getInfoId, false));
  }

  /**
   * 查询系统登录日志集合
   *
   * @param logininfor 访问日志对象
   * @return 登录记录集合
   */
  default Page<SysLogininfor> selectLogininforPage(SysLogininfor logininfor) {
    return paginate(
        PageUtils.getPage(),
        QueryWrapper.create(
                logininfor,
                SqlOperators.of(SysLogininfor::getUserName, SqlOperator.LIKE)
                    .of(SysLogininfor::getIpaddr, SqlOperator.LIKE))
            .ge(
                SysLogininfor::getLoginTime,
                logininfor.getParams().get("beginTime"),
                logininfor.getParams().get("beginTime") != null)
            .le(
                SysLogininfor::getLoginTime,
                logininfor.getParams().get("endTime"),
                logininfor.getParams().get("endTime") != null)
            .orderBy(SysLogininfor::getInfoId, false));
  }

  /**
   * 批量删除系统登录日志
   *
   * @param infoIds 需要删除的登录日志ID
   * @return 结果
   */
  default int deleteLogininforByIds(Long[] infoIds) {
    return deleteBatchByIds(ListUtil.of(infoIds));
  }

  /**
   * 清空系统登录日志
   *
   * @return 结果
   */
  @Delete("truncate table sys_logininfor")
  int cleanLogininfor();
}
